<template>
<div><div>
            <van-nav-bar
          title="面经登陆"
          left-arrow
        />
            <van-form @submit="onSubmit" >
          <van-field
            v-model="username"
            name="username"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="password"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px;">
            <van-button round block type="info" native-type="submit" style="background-color: #89c379;border: 1px solid #89c379;" >提交</van-button>
          </div>
          <router-link class="link" to="register">去注册</router-link>
        </van-form>
        </div></div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/api/storage'
export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // 往后台发送注册请求了
      const res = await login(values)
      setToken(res.data.token)
      console.log(res)
      this.$toast.success('登录成功')
      this.$router.push('/')
    }
  }
}
</script>

<style>
.link {
color: #069;
font-size: 18px;
padding-right: 20px;
float: right;
}
</style>
